/*
 * @Author: xiaosihan
 * @Date: 2024-10-31 00:17:31
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-31 02:56:34
 */


import { autorun, toJS } from "mobx";
import { ReactNode, useEffect, useRef, useState } from "react";
import styles from "./institutionalRisk.module.less";
import Title3 from "@components/Title3/Title3";
import clsx from "clsx";
import doubtfulSituationStore from "../doubtfulSituationStore";
import centerTopStore from "../CenterTop/centerTopStore";
import pdfStore from "../PDFModal/pdfStore";
import largeScreenApi from "@api/largeScreenApi";
import axios from "axios";

interface Iprops {
    children?: ReactNode
}


//提取A字符窜中包含的B字符串的数量
function AincludesB(A: string = "", B: string = "") {
    let total = 0;
    B.split("").map(b => {
        A.includes(b) && (total++);
    })
    return total;
}

// 机构风险指数
export default function InstitutionalRisk(props: Iprops) {

    const dom = useRef<HTMLDivElement | null>(null);
    const [data, setData] = useState([]);

    useEffect(() => autorun(async () => {
        const { active } = centerTopStore;

        //这个是本地有pdf数据的json
        // toJS(doubtfulSituationStore.institutional_risk_index);
        let JSONRes = await axios.get("./intelligent_check/institutional_risk_index.json");
        let pdfData = JSONRes.data;

        let res = await largeScreenApi.institutional_risk_index() as any;
        let data = res.list;
        // debugger;

        let modelList = toJS(doubtfulSituationStore.regulatory_model_List);

        // 根据模型来筛选
        if (modelList[active]) {
            data = data.filter((d: any) => d.checkout_model.includes(modelList[active].model_name));
        }

        data.map((d: any) => {
            let similarity = 0;
            //提取pdf
            pdfData.map((pdfd: any) => {
                let s = AincludesB(pdfd.pdf, d.admdvs_name + d.checkout_model);
                if (s > similarity) {
                    similarity = s;
                    d.pdf = pdfd.pdf;
                }
            });
        })

        setData(data);

    }, { delay: 10 }), []);

    return (
        <div className={styles.institutionalRisk} >
            <Title3 left={0} top={-70} >机构风险指数</Title3>

            <div className={styles.head}>
                <span className={styles.value1} >监管对象</span>
                <span className={styles.value2} >机构所属的医保区划</span>
                <span className={styles.value3} >风险分10分制</span>
                <span className={styles.value4} >核查状态</span>
            </div>
            <div className={styles.autoScroll}>
                {
                    (data || []).map((d: any, i: number) => {
                        return (
                            <div key={i} className={styles.listItem} >
                                <span className={styles.value1} >{d.nurscare_org_name}</span>
                                <span className={styles.value2} >{d.admdvs_name}</span>
                                <span className={styles.value3} >{d.risk_sco}</span>
                                <span className={styles.value4} >
                                    {
                                        d.chk_stas === "已核查" ?
                                            <span
                                                className={styles.pdfBtn}
                                                onClick={() => {
                                                    if (d.pdf) {
                                                        pdfStore.setPdfUrl(`./${d.pdf}`);
                                                    }
                                                }}
                                            >{d.chk_stas}</span> :
                                            d.chk_stas
                                    }
                                </span>
                            </div>
                        );
                    })
                }
            </div>
        </div>
    );

}